﻿using Modbus.Data;
using Modbus.Device;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Nmodbus创建slave
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private TcpListener listener;
        private ModbusSlave slave;

        private void button1_Click(object sender, EventArgs e)
        {
            listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 666);
            listener.Start();

            slave = ModbusTcpSlave.CreateTcp(1, listener);

            slave.DataStore = DataStoreFactory.CreateDefaultDataStore();
            slave.DataStore.DataStoreWrittenTo += DataStore_DataStoreWrittenTo;
            slave.ModbusSlaveRequestReceived += Slave_ModbusSlaveRequestReceived; ;
            slave.WriteComplete += Slave_WriteComplete; ;



        }

        private void Slave_WriteComplete(object sender, ModbusSlaveRequestEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void Slave_ModbusSlaveRequestReceived(object sender, ModbusSlaveRequestEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void DataStore_DataStoreWrittenTo(object sender, DataStoreEventArgs e)
        {
            throw new NotImplementedException();
        }


      
        private void WriteInputDiscretes(int id, bool value)
        {
            slave.DataStore.InputDiscretes[id] = value;
        }

        private void WriteToCoil(int id, bool value)
        {
            slave.DataStore.CoilDiscretes[id] = value;
        }
        private void WriteInputRegisters()
        {

        }
        private void WriteHoldingRegisters()
        {

        }

        private void ReadInputs()
        {

        }

        private void ReadCoil()
        {

        }
        private void ReadInputRegisters()
        {

        }
        private void ReadHoldingRegisters()
        {

        }
    }
}
